<html>
 <!--
 	@title  Auto-Reload
 	@author Jean-Lou Dupont
 	@desc   Background page for Auto-Reload
  -->
	<head>
		<script>
			var pageActionId = "auto_reload";
			var tabParams = {};
			var icons  = {"true": 1, "false": 0};
			var titles = {"true": "Click to stop auto-reload", 
						"false":"Click to start auto-reload"};
		
			chrome.extension.onConnect.addListener(function(port) {				
				console.log("* Auto-Reload: on-connect, tabId:"+port.tab.id);
				
		    	var tid=port.tab.id;
		    	var params = tabParams[tid]     || {};
		    	var urls   = params["urls"]     || {};
				var state  = urls[port.tab.url] || false;			

				// Cases:
				// 1- new tab
				// 2- page load / returning through 'back' button
				urls[port.tab.url] = state;
				params["urls"]     = urls;			
				params["port"]     = port;
				tabParams[tid]     = params;
				
				//we just received this 'connect' request so
				//the port must be ok to use: assume safe.
				var timeout=localStorage["timeout"];
				var cmd={"state":state, "timeout":timeout};
				
				port.postMessage(cmd);
			    
		        chrome.pageActions.enableForTab(pageActionId, {tabId: tid,
																url: port.tab.url,
																title: titles[String(state)],
																iconId: icons[String(state)]});
			});

			// If the user navigates to a different location,
			//  he/she probably don't want to keep re-loading the new target.
			chrome.tabs.onUpdated.addListener(function(tabId, changeInfo) {

				var urlChanged=changeInfo["url"] || false;
				if (urlChanged === false)
					return;

				//not sure if this can happen...
				var params=tabParams[tabId] || null;
				if (params===null)
					return;

				//at this point, the tab's parameters were already set
				//previously and we need to update.
				var urls  = params["urls"];
				var state = urls[urlChanged] || false;

				//something wrong here... shouldn't happen
				//but hey, I am being paranoid ;-)
				var port=params["port"] || null;
				if (null===port)  
					return;

				console.log("* Auto-Reload: tab<"+tabId+"> updated, state:"+state);

				var timeout=localStorage["timeout"];
				var cmd={"state":state, "timeout":timeout};
				
				port.postMessage(cmd);
			});
			
			chrome.pageActions[pageActionId].addListener(function(paId, pageActionInfo) {
				chrome.tabs.get(pageActionInfo.tabId, function(tab) {
					// We need to know if we are the active window, because the tab may
					// have moved to another window and we don't want to execute this
					// action multiple times.
					var url=tab.url || "";
				    var params = tabParams[tab.id] || {};
				    var urls   = params["urls"]    || {};
				    var state  = urls[url]         || false;
				    var port   = params["port"]    || null;
				    
				    var new_state=state ? false:true;
				    urls[url] = new_state;
				    
				    var new_params={"port":port, "urls":urls};
				    tabParams[tab.id] = new_params;

					//v5
					var timeout=localStorage["timeout"];
					var cmd={"state":new_state, "timeout":timeout};
				    
				    //console.log( new_params );
				    if (port) {
						port.postMessage(cmd);
						console.log("* Auto-Reload: Sent Message - tabId<"+tab.id+">  state<"+new_state+">");
						
						chrome.pageActions.enableForTab(pageActionId, {tabId: tab.id,
														url: port.tab.url,
														title: titles[String(new_state)],
														iconId: icons[String(new_state)]});
				    } else {
						console.log("! Auto-Reload: CANNOT send - tabId<"+tab.id+">  state<"+new_state+">");
					}
				});
			});
		</script>
	</head>
</html>